package com.example.administrator.printoutindemo.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.adapter.InKuAdapter_ZhiJianWanCheng;
import com.example.administrator.printoutindemo.bean.InKuBean_ZhiJianWanCheng;
import com.example.administrator.printoutindemo.di.component.DaggerRuKu_ZhiJianWanChengComponent;
import com.example.administrator.printoutindemo.di.module.RuKu_ZhiJianWanChengModule;
import com.example.administrator.printoutindemo.http.SPUtil;
import com.example.administrator.printoutindemo.mvp.contract.RuKu_ZhiJianWanChengContract;
import com.example.administrator.printoutindemo.mvp.presenter.RuKu_ZhiJianWanChengPresenter;
import com.example.administrator.printoutindemo.mvp.ui.entity.RuKu_ZhiJianWanCheng;
import com.example.administrator.printoutindemo.mvp.ui.view.RuKuRightSideslipLay;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.addapp.pickers.common.LineConfig;
import cn.addapp.pickers.picker.DateTimePicker;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ACTIVITY_RU_KU_ZHI_JIAN_WAN_CHENG)
public class RuKu_ZhiJianWanChengActivity extends BaseActivity<RuKu_ZhiJianWanChengPresenter> implements RuKu_ZhiJianWanChengContract.View, RuKuRightSideslipLay.FlowingDrawerInterface, OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.btnRight)
    TextView mBtnRight;
    @BindView(R.id.drawer_layout)
    DrawerLayout mDrawer;
    @BindView(R.id.nav_view)
    LinearLayout navigationView;
    @BindView(R.id.empty)
    View mEmptyLayout;
    @BindView(R.id.empty_text)
    TextView empty_text;
    @BindView(R.id.empty_image)
    ImageView empty_image;
    @BindView(R.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    @BindView(R.id.et_kai_shi_shi_jian)
    TextView etKaiShiShiJian;
    @BindView(R.id.et_jie_shu_shi_jian)
    TextView etJieShuShiJian;
    @BindView(R.id.btn_cha_xun)
    Button btnChaXun;
    private EditText et_ddh, et_ddmc, et_xmbh, et_wlmc, et_gys;
    private ListView lv_liebiao;
    private TextView tv_chaxun, tv_weizhi;
    private List<InKuBean_ZhiJianWanCheng> mlist;
    private InKuAdapter_ZhiJianWanCheng mAdapter;
    private RuKuRightSideslipLay menuHeaderView;

    private String mSearchDingDanHao = "";
    private String mSearchXiangMuMingCheng = "";
    private String mSearchXiangMuBianMa = "";
    private String mSearchWuLiaoMingCheng = "";
    private String mSearchGongYingShangMingCheng = "";
    private String mKaiShiShiJian = "";
    private String mJieShuShiJian = "";

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerRuKu_ZhiJianWanChengComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .ruKu_ZhiJianWanChengModule(new RuKu_ZhiJianWanChengModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_ru_ku__zhi_jian_wan_cheng; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_ru_ku__zhi_jian_wan_cheng));
        //getRuKu_ZhiJianWanChengData();

        initBtnRight();
//        getRuKuData();
        initEmpty();
        initView1();
        initRefreshLayout();

        mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);

        menuHeaderView = new RuKuRightSideslipLay(this, this, this);
        navigationView.addView(menuHeaderView);

        menuHeaderView.setCloseMenuCallBack(new RuKuRightSideslipLay.CloseMenuCallBack() {
            @Override
            public void setupCloseMean() {
                closeMenu();
            }
        });


        getDistributeList("", "", "", "", "", "", "");
    }

    private void initBtnRight() {
        mBtnRight.setText("筛选");
        mBtnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMenu();
            }
        });
    }

    private void initEmpty() {
        empty_image.setImageResource(com.xiaoxing.zhinenghuacangku.R.drawable.ic_empty);
        empty_text.setTextColor(Color.parseColor("#CFCFCF"));
        empty_text.setText("暂无数据");
    }

    private void initView1() {
        et_ddh = findViewById(R.id.et_ddh);
        et_ddmc = findViewById(R.id.et_ddmc);
        et_wlmc = findViewById(R.id.et_wlmc);
        et_xmbh = findViewById(R.id.et_xmbh);
        et_gys = findViewById(R.id.et_gys);
        lv_liebiao = findViewById(R.id.lv_liebiao);
        mlist = new ArrayList<>();
        mAdapter = new InKuAdapter_ZhiJianWanCheng(getContext(), mlist);
        lv_liebiao.setAdapter(mAdapter);
        tv_chaxun = findViewById(R.id.tv_chaxun);
        tv_chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//        getDistributeList(et_ddh.getText().toString(), et_ddmc.getText().toString(), et_wlmc.getText().toString(), et_xmbh.getText().toString(), et_gys.getText().toString());

            }
        });
    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new
                ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(com.xiaoxing.zhinenghuacangku.R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadMoreListener(this);
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setEnableLoadMore(true);
    }

    public void closeMenu() {
        mDrawer.closeDrawer(GravityCompat.END);
    }

    /**
     * @param
     * @return
     * @method
     * @description 质检物资入库  方法GetDistributeListPaging 改为  GetDistributeListPagingQuality
     * <p>
     * 说明：待入库 统一调用 :GetDistributeListPagingQuality
     * 用法同GetDistributeListPaging
     * 返回实体中 多了一个字段 int isqualityplan( 0 表示无质检计划 1表示有质检计划)
     * @date: 2020/7/13 0013 11:16
     * @author: 小星
     */
    private void getDistributeList(String dingDanHao, String xiangMuMingCheng, String xiangMuBianMa, String wuLiaoMingCheng, String gongYingShangMingCheng, String startTime, String endTime) {

        CProgressDialogUtils.showProgressDialog(this);
        String methodName = "GetQualityCompletedInstoreMList";
        //质检物资入库

        SoapParams params = new SoapParams();
        params.put("user_id", SPUtil.getString(getContext(), "iduser", ""));
        params.put("purchaseid", dingDanHao);
        params.put("projectName", xiangMuMingCheng);
        params.put("material", wuLiaoMingCheng);
        params.put("projectNo", xiangMuBianMa);
        params.put("sname", gongYingShangMingCheng);
        params.put("supplydatebegin", startTime);
        params.put("supplydateend", endTime);
        params.put("startIndex", String.valueOf(mStart));
        params.put("endIndex", String.valueOf(mStart + mNum - 1));

        /**
         * 请求
         */
        String finalMethodName = methodName;
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();
                String result = object.getProperty(finalMethodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    mAdapter.shua();
                    List<InKuBean_ZhiJianWanCheng> inKuBean = new Gson().fromJson(result, new TypeToken<List<InKuBean_ZhiJianWanCheng>>() {
                    }.getType());
                    if (inKuBean != null && inKuBean.size() > 0) {
                        mStart += inKuBean.size();
                        mEmptyLayout.setVisibility(View.GONE);
                        lv_liebiao.setVisibility(View.VISIBLE);
                        mAdapter.addAll(inKuBean);
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
                ArmsUtils.makeText(getContext(), error.getMessage());

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());

    }

    public void openMenu() {
        mDrawer.openDrawer(GravityCompat.END);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        getDistributeList(mSearchDingDanHao, mSearchXiangMuMingCheng, mSearchXiangMuBianMa, mSearchWuLiaoMingCheng, mSearchGongYingShangMingCheng, mKaiShiShiJian, mJieShuShiJian);
        mRefreshLayout.finishLoadMore();
    }

    @Override
    public void onBackPressed() {

    }


    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);

    }


    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog();

    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }


    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @Override
    public void flowingDrawerClose() {
        closeMenu();
    }

    @Override
    public void search(String dingDanHao, String xiangMuMingCheng, String xiangMuBianMa, String wuLiaoMingCheng, String gongYingShangMingCheng, String startTime, String endTime) {
        mEmptyLayout.setVisibility(View.VISIBLE);
        lv_liebiao.setVisibility(View.GONE);
        mAdapter.clearData();
        mStart = 1;
        closeMenu();
        mSearchDingDanHao = TextUtils.isEmpty(dingDanHao) ? "" : dingDanHao;
        mSearchXiangMuMingCheng = TextUtils.isEmpty(xiangMuMingCheng) ? "" : xiangMuMingCheng;
        mSearchXiangMuBianMa = TextUtils.isEmpty(xiangMuBianMa) ? "" : xiangMuBianMa;
        mSearchWuLiaoMingCheng = TextUtils.isEmpty(wuLiaoMingCheng) ? "" : wuLiaoMingCheng;
        mSearchGongYingShangMingCheng = TextUtils.isEmpty(gongYingShangMingCheng) ? "" : gongYingShangMingCheng;
        mKaiShiShiJian = TextUtils.isEmpty(startTime) ? "" : startTime;
        mJieShuShiJian = TextUtils.isEmpty(endTime) ? "" : endTime;
        mAdapter.clearData();
        getDistributeList(dingDanHao, xiangMuMingCheng, xiangMuBianMa, wuLiaoMingCheng, gongYingShangMingCheng, startTime, endTime);
    }

    @OnClick({R.id.et_kai_shi_shi_jian, R.id.et_jie_shu_shi_jian, R.id.btn_cha_xun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et_kai_shi_shi_jian:
                onYearMonthDayTimePicker(etKaiShiShiJian);
                break;
            case R.id.et_jie_shu_shi_jian:
                onYearMonthDayTimePicker(etJieShuShiJian);

                break;
            case R.id.btn_cha_xun:
                mKaiShiShiJian = etKaiShiShiJian.getText().toString().trim();
                mJieShuShiJian = etJieShuShiJian.getText().toString().trim();

                if (TextUtils.isEmpty(mKaiShiShiJian)) {
                    showMessage("请选择开始时间");
                    return;
                }
                if (TextUtils.isEmpty(mJieShuShiJian)) {
                    showMessage("请选择结束时间");
                    return;
                }
                mEmptyLayout.setVisibility(View.VISIBLE);
                lv_liebiao.setVisibility(View.GONE);
                mAdapter.clearData();
                mStart = 1;
                getDistributeList(mSearchDingDanHao, mSearchXiangMuMingCheng, mSearchXiangMuBianMa, mSearchWuLiaoMingCheng, mSearchGongYingShangMingCheng, mKaiShiShiJian, mJieShuShiJian);
                break;
        }
    }

    public void onYearMonthDayTimePicker(TextView view) {

        DateTimePicker picker = new DateTimePicker(this, DateTimePicker.HOUR_24);

        Calendar now = Calendar.getInstance();

        picker.setCancelText("取消");
        picker.setSubmitText("确定");
        picker.setActionButtonTop(true);
        picker.setDateRangeStart(1900, 1, 1);
        picker.setDateRangeEnd(2500, 12, 31);
        picker.setSelectedItem(now.get(Calendar.YEAR), (now.get(Calendar.MONTH) + 1), now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE));
        picker.setTimeRangeStart(0, 0);
        picker.setTimeRangeEnd(23, 59);
        picker.setCanLinkage(false);
        picker.setTitleText("请选择");
//        picker.setStepMinute(5);
        picker.setWeightEnable(true);
        picker.setWheelModeEnable(true);
        LineConfig config = new LineConfig();
        config.setColor(Color.BLACK);//线颜色
        config.setAlpha(120);//线透明度
        config.setVisible(true);//线不显示 默认显示
        picker.setLineConfig(config);
        picker.setLabel("年", "月", "日", "时", "分");
        picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
            @Override
            public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
//                ArmsUtils.snackbarText(year + "-" + month + "-" + day + " " + hour + ":" + minute);
                view.setText(year + "-" + month + "-" + day + " " + hour + ":" + minute);
            }
        });
        picker.show();

    }

    @Override
    public void getRuKu_ZhiJianWanChengDataSuccess(RuKu_ZhiJianWanCheng entityData) {


    }

    private void getRuKu_ZhiJianWanChengData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getRuKu_ZhiJianWanChengData(map);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

    }
}
